package hu.arguscab.ai.game;

import hu.arguscab.ai.Edge;
import hu.arguscab.ai.Model;
import hu.arguscab.ai.Node;
import hu.arguscab.ai.NodeCreator;
import hu.arguscab.ai.NodePair;
import hu.arguscab.game.CompoundAction;
import hu.arguscab.game.GameAction;
import hu.arguscab.game.GameCompoundAction;
import hu.arguscab.game.Ship;
import hu.arguscab.game.ShipState;
import hu.arguscab.game.WorldState;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author sharp
 */
public class GameNodeCreator extends NodeCreator<CompoundAction, Model> {

	// Spring
	private int throttleCost;
	private int breakCost;
	private int leftCost;
	private int rightCost;
	private int haltCost;
	/////////////////////////////////////////////
	private Ship myShip;

	@Override
	public Model createRoot() {
		WorldState now = new WorldState();
		ShipState shipState = myShip.getState();
		shipState.setCollision(false);
		now.setShipState( shipState );
		return new Model(now);
	}

	@Override
	public NodePair<CompoundAction, Model> createNode(Node<Model> from, int step) {
		throw new UnsupportedOperationException();
	}

	@Override
	public List<NodePair<CompoundAction, Model>> createNodes(Node<Model> from) {
		List<NodePair<CompoundAction, Model>> list = new ArrayList<NodePair<CompoundAction, Model>>();
		Model now = from.getData();
		ShipState nowState = ((WorldState)now.getState()).getShipState();
		for ( GameAction ac : GameAction.values() ) {
			GameCompoundAction action = new GameCompoundAction();
			action.addAction(ac);

			// hajo
			ShipState state = nowState.getByActions(action, getDepth());
			// vilagban az uj hajo pozicio
			WorldState wstate = new WorldState(state);
			// model, világot tárolja
			Model model = new Model( wstate);
			// node, modelt tárolja
			Node<Model> to = new Node<Model>(model);

			GameEdge edge = new GameEdge();
			edge.setData(action);

			NodePair<CompoundAction, Model> pair = new NodePair<CompoundAction, Model>(from, to, edge);

			list.add(pair);
		}
		return list;
	}

	@Override
	public int calcEdgeCost(Edge<CompoundAction> edge) {
		CompoundAction actions = edge.getData();
		boolean isHalting = true;
		int cost = 0;
		if ( actions.hasAction(GameAction.THROTTLE) ) {
			cost += throttleCost;
			isHalting = false;
		}
		if ( actions.hasAction(GameAction.BREAK) ) {
			cost += breakCost;
			isHalting = false;
		}
		if ( actions.hasAction(GameAction.LEFT) ) {
			cost += leftCost;
			isHalting = false;
		}
		if ( actions.hasAction(GameAction.RIGHT) ) {
			cost += rightCost;
			isHalting = false;
		}
		if ( isHalting )
			cost = haltCost;
		return cost;
	}

	public void setBreakCost(int breakCost) {
		this.breakCost = breakCost;
	}

	public void setHaltCost(int haltCost) {
		this.haltCost = haltCost;
	}

	public void setLeftCost(int leftCost) {
		this.leftCost = leftCost;
	}

	public void setRightCost(int rightCost) {
		this.rightCost = rightCost;
	}

	public void setThrottleCost(int throttleCost) {
		this.throttleCost = throttleCost;
	}

	public void setShip(Ship s) {
		myShip = s;
	}

}
